package com.joysuch.wwyt.bp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.bp.bean.investigate.BpInvestigateRecordBean;
import com.joysuch.wwyt.bp.bean.investigate.BpInvestigateRecordHisQueryBean;
import com.joysuch.wwyt.bp.bean.investigate.BpInvestigateRecordQueryBean;
import com.joysuch.wwyt.bp.bean.investigate.vo.BpInvestigateRecordHisVO;
import com.joysuch.wwyt.bp.bean.investigate.vo.BpInvestigateRecordVO;
import com.joysuch.wwyt.bp.bean.investigate.vo.TaskItemMapperVo;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigateRecord;
import com.joysuch.wwyt.bp.service.BpInvestigateRecordHisService;
import com.joysuch.wwyt.bp.service.BpInvestigateRecordService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import com.joysuch.wwyt.repeatsubmit.NoRepeatSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/3/25 16:19
 */
@Controller
@RequestMapping("/bp/investigate/record")
@Api(tags = "隐患管理-隐患排查记录")
public class BpInvestigateRecordController {

    @Autowired
    private BpInvestigateRecordService bpInvestigateRecordService;

    @Autowired
    private BpInvestigateRecordHisService bpInvestigateRecordHisService;

    @PostMapping("save")
    @RequiresPermissions("bp:investigate:record:save")
    @ApiOperation("新增/修改排查记录")
    @ResponseBody
    public ResultBean save(@RequestBody @Valid BpInvestigateRecordBean bean) {
        return bpInvestigateRecordService.save(bean);
    }

    @PostMapping("saveV2")
//    @RequiresPermissions("bp:investigate:record:saveV2")
    @ApiOperation("新增/修改排查记录-新版本")
    @ResponseBody
    @NoRepeatSubmit(paramNames = "taskId")
    public ResultBean saveV2(@RequestBody @Valid BpInvestigateRecordVO recordVO) throws Exception {
        try {
            return ResultBean.success(bpInvestigateRecordService.saveV2(recordVO));
        } catch (ApiBusinessException ae) {
            return ResultBean.fail(ae.getCode(), ae.getMessage());
        }
    }

    @PostMapping("saveV3")
//    @RequiresPermissions("bp:investigate:record:saveV2")
    @ApiOperation("新增/修改排查记录-新版本-批量保存")
    @ResponseBody
    @NoRepeatSubmit(paramNames = "taskId")
    public ResultBean saveV2(@RequestBody @Valid List<BpInvestigateRecordVO> recordVOList) throws Exception {
        try {
            return ResultBean.success(bpInvestigateRecordService.saveV3(recordVOList));
        } catch (Exception ae) {
            return ResultBean.fail(1002, ae.getMessage());
        }
    }

    @GetMapping("position-check")
    @ApiOperation("")
    @ResponseBody
    public ResultBean positionCheck(Long userId, Long pointId, Long taskId) throws Exception {
        return ResultBean.success(bpInvestigateRecordService.positionCheck(userId, pointId, taskId));
    }

    @PostMapping("saveBatch")
    @RequiresPermissions("bp:investigate:record:saveBatch")
    @ApiOperation("批量新增/修改排查记录")
    @ResponseBody
    public ResultBean saveBatch(@RequestBody List<BpInvestigateRecordVO> bean) {
        List<TaskItemMapperVo> taskItemMapperVoList = bpInvestigateRecordService.saveBatch(bean);
        return ResultBean.success(taskItemMapperVoList);
    }

    @GetMapping("/delete/{id}")
    @RequiresPermissions("bp:investigate:record:delete")
    @ApiOperation(value = "根据排查记录Id删除")
    @ApiImplicitParam(name = "id", value = "排查记录Id", paramType = "path", required = true)
    @ResponseBody
    public ResultBean delete(@PathVariable Long id) {
        return bpInvestigateRecordService.delete(id);
    }

    @GetMapping("/deleteByIds")
    @RequiresPermissions("bp:investigate:record:delete")
    @ApiOperation(value = "根据排查记录Id批量删除")
    @ResponseBody
    public ResultBean deleteByIds(List<Long> ids) {
        return bpInvestigateRecordService.deleteByIds(ids);
    }

    @GetMapping("/page")
    @ApiOperation(value = "排查记录分页查询", response = BpInvestigateRecord.class)
    @ResponseBody
    public ResultBean page(BpInvestigateRecordHisQueryBean bean) {
        //return bpInvestigateRecordService.page(bean, 0);
        Page<BpInvestigateRecordHisVO> recordHisVOPage = bpInvestigateRecordHisService.pageByCheckPlanId(bean);
        return ResultBean.pageData(recordHisVOPage.getRecords(),recordHisVOPage.getTotal());
    }

    @GetMapping("/excel-export")
    @ApiOperation("排查记录导出")
    @RequiresPermissions("bp:investigate:record:excel-export")
    public void excelExport(BpInvestigateRecordQueryBean bean, HttpServletRequest request, HttpServletResponse response) {
        bpInvestigateRecordService.excelExport(bean, request, response);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "排查记录根据主键查询", response = BpInvestigateRecord.class)
    @ApiImplicitParam(name = "id", value = "排查记录id", paramType = "path", required = true)
    @ResponseBody
    public ResultBean getById(@PathVariable Long id) {
        return bpInvestigateRecordService.getById(id, 0);
    }

    @GetMapping("/exceptionHandle/{id}")
    @RequiresPermissions("bp:investigate:record:exceptionHandle")
    @ApiOperation(value = "异常处理-根据记录id", response = BpInvestigateRecord.class)
    @ApiImplicitParam(name = "id", value = "排查记录id", paramType = "path", required = true)
    @ResponseBody   
    public ResultBean exceptionHandle(@PathVariable Long id) {
        return bpInvestigateRecordService.exceptionHandle(id);
    }

    @GetMapping("/repair/{recordId}/{checkItemId}")
    @ApiOperation(value = "异常处理-整改")
    @ApiImplicitParam(name = "recordId", value = "排查记录ID", paramType = "path", required = true)
    @ResponseBody
    public ResultBean repair(@PathVariable Long recordId, @PathVariable Long checkItemId) {
        return bpInvestigateRecordService.repair(recordId, checkItemId);
    }

    @GetMapping("/repairV2/{itemId}")
    @ApiOperation(value = "异常处理-整改")
    @ApiImplicitParam(name = "recordId", value = "排查记录ID", paramType = "path", required = true)
    @ResponseBody
    public ResultBean repairV2(@PathVariable Long itemId) {
        bpInvestigateRecordService.repairV2(itemId);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/repairNewWf/{itemId}/{wfProcessInsId}/{wfProcessStatus}")
    @ApiOperation(value = "异常处理-新版流程引擎")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", value = "排查记录itemID", required = true),
            @ApiImplicitParam(name = "wfProcessInsId", value = "新版流程实例id", required = true),
            @ApiImplicitParam(name = "wfProcessStatus", value = "流程状态", required = true)
    })
    @ResponseBody
    public ResultBean repairNewWf(@PathVariable Long itemId, @PathVariable String wfProcessInsId, @PathVariable String wfProcessStatus) {
        try {
            return ResultBean.success(bpInvestigateRecordService.repairNewWf(itemId, wfProcessInsId, wfProcessStatus));
        } catch (ApiCoreException e) {
            return ResultBean.fail(e.getCode(),e.getMessage());
        }
    }

}
